<?php
    session_start();
    include('top.php');
    include('../sql.php');
    $web_info = "select * from webinfo";
    $web_info_result = $link->query($web_info);
    $web_info_row = mysqli_fetch_array($web_info_result);
?>
<div class="home-container-body">
    <?php include('nav.php'); ?>
</div>

<div class="admin_hone-content">
    <form id="infoForm">
        <table width="100%">
            <tr>
                <td>管理员</td>
                <td><input type="text" name="user"></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="添加管理员">
                    <div id="sResult"></div>
                </td>
            </tr>
        </table>
    </form>
    <form id="passForm" style="margin-top: 12px;">
        <table width="100%">
            <tr>
                <td>密码</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="修改">
                    <div id="passResult"></div>
                </td>
            </tr>
        </table>
    </form>
</div>
<script type="text/javascript" src="../static/js/jquery-3.6.0.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#infoForm').on('submit', function(e){
            e.preventDefault();
            var formData = $(this).serialize();

            $.ajax({
                url: 'control.php?action=admin',
                type: 'POST',
                data: formData,
                success: function(response){
                    $('#sResult').empty();
                    $('#sResult').html(response);
                    location.reload();
                },
                error: function(error){
                    console.log(error);
                    $('#sResult').html('失败，请稍后再试。');
                }
            });
        });
    });
    $(document).ready(function(){
        $('#passForm').on('submit', function(e){
            e.preventDefault();
            var formData = $(this).serialize();

            $.ajax({
                url: 'control.php?action=pass',
                type: 'POST',
                data: formData,
                success: function(response){
                    $('#passResult').empty();
                    $('#passResult').html(response);
                    location.reload();
                },
                error: function(error){
                    console.log(error);
                    $('#passResult').html('失败，请稍后再试。');
                }
            });
        });
    });
</script>
<?php include('bottom.php'); ?>